﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.Utility;
using TimeCard.Core;

namespace TimeCard
{
    public partial class frmRuleManage : Form
    {
        public frmRuleManage()
        {
            InitializeComponent();
        }

        private void BindGrid()
        {
            gridRule.AutoGenerateColumns = false;
            var list = ApiService.UserRuleService.GetList();
            gridRule.DataSource = DataTableHelper.ConvertToDataTable(list);
        }

        private void BindForm(UserRule model)
        {
            numStartHour.Value = model.StartHour;
            numStartMinute.Value = model.StartMinute;
            numEndHour.Value = model.EndHour;
            numEndMinute.Value = model.EndMinute;
            txtUsers.Text = model.Users;
            cbHasWeekend.Checked = model.HasWeekend;
        }

        private UserRule GetModel()
        {
            var model = new UserRule();
            model.StartHour = (int)numStartHour.Value;
            model.StartMinute = (int)numStartMinute.Value;
            model.EndHour = (int)numEndHour.Value;
            model.EndMinute = (int)numEndMinute.Value;
            model.Users = txtUsers.Text.Trim();
            model.HasWeekend = cbHasWeekend.Checked;
            return model;
        }

        private int GetCurrentId()
        {
            if (gridRule.CurrentRow == null) return -1;
            return gridRule.CurrentRow.Cells[0].Value.ToInt();
        }

        private BoolMessage Valid(UserRule model)
        {
            var start = new TimeSpan(model.StartHour, model.StartMinute, 0).Ticks;
            var end = new TimeSpan(model.EndHour, model.EndMinute, 0).Ticks;
            if (start >= end)
            {
                return new BoolMessage(false, "上班时间不能小于等于下班时间");
            }
            return BoolMessage.True;
        }

        private void frmRuleManage_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            var model = GetModel();
            if (ApiService.UserRuleService.Exists(model))
            {
                MessageBox.Show("输入的规则已经存在");
                return;
            }
            var result = Valid(model);
            if (result.Failure)
            {
                MessageBox.Show(result.Message);
                return;
            }
            ApiService.UserRuleService.Add(model);
            BindGrid();
            gridRule.CurrentCell = gridRule.Rows[gridRule.Rows.Count - 1].Cells[1];
            var id = GetCurrentId();
            BindForm(ApiService.UserRuleService.Get(id));
            MessageBox.Show("保存成功");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var model = GetModel();
            model.Id = GetCurrentId();
            if (ApiService.UserRuleService.Exists(model))
            {
                MessageBox.Show("输入的规则已经存在");
                return;
            }
            var result = Valid(model);
            if (result.Failure)
            {
                MessageBox.Show(result.Message);
                return;
            }
            ApiService.UserRuleService.Edit(model);
            var rowIndex = gridRule.CurrentCell.RowIndex;
            BindGrid();
            gridRule.CurrentCell = gridRule.Rows[rowIndex].Cells[1];
            MessageBox.Show("保存成功");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridRule.SelectedCells.Count == 0)
            {
                MessageBox.Show("请选中需要删除的行");
                return;
            }
            if (MessageBox.Show("确定要删除选定的规则吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) != DialogResult.Yes)
            {
                return;
            }
            foreach (DataGridViewCell cell in gridRule.SelectedCells)
            {
                var id = gridRule.Rows[cell.RowIndex].Cells[0].Value.ToInt();
                ApiService.UserRuleService.Delete(id);
            }
            BindGrid();
        }

        private void gridRule_SelectionChanged(object sender, EventArgs e)
        {
            if (gridRule.SelectedCells.Count == 0)
            {
                btnDelete.Enabled = false;
                btnSave.Enabled = false;
                BindForm(new UserRule());
            }
            else if (gridRule.SelectedCells.Count == 1)
            {
                btnDelete.Enabled = true;
                btnSave.Enabled = true;
                var id = GetCurrentId();
                var model = ApiService.UserRuleService.Get(id);
                BindForm(model);
            }
            else
            {
                btnDelete.Enabled = true;
                btnSave.Enabled = false;
            }
        }
    }
}
